package com.Web.guardians;

import com.DAO.Impl.guardiansDAOImpl;
import com.entity.guardians;
import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 20x
 * @date 2024 - 12 -11
 **/
@WebServlet("/AddGuardian")
public class AddGuardiansServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        System.out.println("AddGuardian");
        PrintWriter out = resp.getWriter();
        Gson gson = new Gson();
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        String phone = req.getParameter("phone");
        String avatar = req.getParameter("avatar");
        guardians guardians = new guardians(name, age, gender, phone, avatar);
        System.out.println(guardians);
        int count = new guardiansDAOImpl().add(guardians);
        System.out.println(count);
        if(count > 0){
            out.print(gson.toJson("添加成功"));
        }else{
            out.print(gson.toJson("添加失败"));
        }
    }
}
